package cn.ronghuanet.org.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RonghuaPermission(name = "部门管理",descs = "部门管理api")
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentServiceI service;

    // 增
    @RonghuaPermission(name = "部门新增/修改",descs = "部门新增和修改是一个接口")
    @PostMapping
    public AjaxResult insert(@RequestBody Department department){
        try {
            if (Objects.nonNull(department)){
                if (Objects.nonNull(department.getId())){
                    service.update(department);
                }else{
                    service.insert(department);
                }
                return AjaxResult.success();
            }
            return AjaxResult.error("参数异常");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    // 删
    @RonghuaPermission(name = "删除指定部门")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @RonghuaPermission(name = "批量删除部门")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 查
    @RonghuaPermission(name = "查询指定部门")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            return AjaxResult.success(service.selectById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @RonghuaPermission(name = "查询所有部门")
    @GetMapping
    public AjaxResult selectAll(){
        try {
            return AjaxResult.success(service.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    // 分页查询的接口
    @RonghuaPermission(name = "部门分页查询")
    @PutMapping
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Department> pageInfo = service.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    // 查
    @RonghuaPermission(name = "查询部门树")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(){
        try {
            List<Department> list = service.deptTree();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
